<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>


	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paramInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="小数位数" align="center" prop="median" />
<!--      <el-table-column label="统计方法" align="center" prop="tjMethod" :formatter="tjMethodFormat"/>-->
      <el-table-column label="参数类型" align="center" prop="paramType" :formatter="paramTypeFormat"/>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            @click="handleParamCode(scope.row)"
          >编码配置</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleParamTxtConvert(scope.row)"
          >文本转换</el-button>

          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数信息配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片" prop="pic">
          <uploadImage v-model="form.pic"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="参数类型" prop="paramType">
          <el-select v-model="form.paramType" placeholder="请选择参数类型">
            <el-option
              v-for="dict in paramTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select v-model="form.unit" placeholder="请选择点位类型">
            <el-option
              v-for="dict in unitOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小数位数" prop="median">
          <el-input-number v-model="form.median" placeholder="请输入小数位" :precision="0" :min="0" :max="4" :step="1" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" placeholder="请输入排序" :precision="0" :min="1" />
        </el-form-item>
        <!--<el-form-item label="统计方法" prop="tjMethod">
          <el-select v-model="form.tjMethod" placeholder="请选择单位">
            <el-option
              v-for="dict in tjMethodOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" :rows="2" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 编码配置对话框 -->
    <el-dialog title="编码配置" :visible.sync="paramCodeOpen" width="740px" append-to-body >
      <div>
        <paramCode :paramId="paramId"></paramCode>
      </div>
    </el-dialog>
    <!-- 文本配置对话框 -->
    <el-dialog title="文本配置" :visible.sync="paramTxtConvertOpen" width="740px" append-to-body >
      <div>
        <paramTxtConvert :paramId="paramId"></paramTxtConvert>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listParamInfo, getParamInfo, delParamInfo, addParamInfo, updateParamInfo, exportParamInfo } from "@/api/basic/paramInfo";
import UploadImage from '@/components/UploadImage';
import paramCode from "../paramCode/index";
import paramTxtConvert from "../paramTxtConvert/index";

export default {
  name: "ParamInfo",
  components: {
    UploadImage,paramCode,paramTxtConvert
  },
  data() {
    return {
      paramId: undefined,
      paramCodeOpen: false,
      paramTxtConvertOpen: false,
      // 单位字典
      unitOptions: [],
      // 参数类型
      paramTypeOptions: [{dictLabel:'数值',dictValue:'num'},{dictLabel:'状态',dictValue:'state'},{dictLabel:'故障',dictValue:'fault'},{dictLabel:'控制',dictValue:'crol'}],
      // 统计方法
      tjMethodOptions: [{dictLabel:'平均值',dictValue:'avg'},{dictLabel:'求差',dictValue:'diff'}],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数信息配置表格数据
      paramInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        unit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        paramType: [
          { required: true, message: "参数类型不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    this.getDicts("basic_unit").then(response => {
      this.unitOptions = response.data;
    });
    this.getList();
  },
  methods: {
    // 参数类型字典翻译
    paramTypeFormat(row, column) {
      return this.selectDictLabel(this.paramTypeOptions, row.paramType);
    },
    // 参数类型字典翻译
    tjMethodFormat(row, column) {
      return this.selectDictLabel(this.tjMethodOptions, row.tjMethod);
    },
    /** 查询参数信息配置列表 */
    getList() {
      this.loading = true;
      listParamInfo(this.queryParams).then(response => {
        this.paramInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        unit: null,
        median: null,
        tjMethod: null,
        paramType: null,
        pic: null,
        remark: null,
        sort: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加参数信息配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getParamInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改参数信息配置";
      });
    },
    /** 编码配置按钮操作 */
    handleParamCode(row) {
      this.paramId = row.id;
      this.paramCodeOpen = true;
    },
    /** 文本配置按钮操作 */
    handleParamTxtConvert(row) {
      this.paramId = row.id;
      this.paramTxtConvertOpen = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateParamInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParamInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除参数【"' + row.name + '】的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delParamInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有参数信息配置数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportParamInfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
